Help with regex

Help with regex

am 13.08.2009 18:52:45 von Barry Brevik

I am using Active Perl 5.8.8. and Perl Dev Kit 7.0.

I'm writing some code that standardizes the format of phone numbers in a
database because users have entered them in every way imaginable.

Anyway, some of the phone numbers look like this:

800-69-VORTEX

....where the user has entered alpha characters instead of numbers. My
first thought was to use a hash to do the translation, like this:

%alpha2num =
(
'A' => '2',
'B' => '2',
'C' => '2',
'D' => '3',
.
.
.

....but since the rest of the routine uses only regular expressions, I
thought that it would be nice if this type of translation could be
accomplished with a regex.

I have not been able to think of a way, but I thought I would put it to
the list to see if anyone else has done this before.

Barry Brevik
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Re: Help with regex

am 13.08.2009 19:21:11 von Stanislaw Romanski

Hello,

Maybe you need something like

$text =~ tr/ABCD/2223/;

?

Stanislaw Romanski


----- Original Message -----
From: "Barry Brevik"
To:
Sent: Thursday, August 13, 2009 6:52 PM
Subject: Help with regex


>I am using Active Perl 5.8.8. and Perl Dev Kit 7.0.
>
> I'm writing some code that standardizes the format of phone numbers in a
> database because users have entered them in every way imaginable.
>
> Anyway, some of the phone numbers look like this:
>
> 800-69-VORTEX
>
> ...where the user has entered alpha characters instead of numbers. My
> first thought was to use a hash to do the translation, like this:
>
> %alpha2num =
> (
> 'A' => '2',
> 'B' => '2',
> 'C' => '2',
> 'D' => '3',
> .
> .
> .
>
> ...but since the rest of the routine uses only regular expressions, I
> thought that it would be nice if this type of translation could be
> accomplished with a regex.
>
> I have not been able to think of a way, but I thought I would put it to
> the list to see if anyone else has done this before.
>
> Barry Brevik
> _______________________________________________
> ActivePerl mailing list
> ActivePerl@listserv.ActiveState.com
> To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
>
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Re: Help with regex

am 13.08.2009 20:21:00 von anthony.okusanya

This is a multipart message in MIME format.
--===============0354187723==
Content-Type: multipart/alternative;
boundary="=_alternative 0064DFF986257611_="

This is a multipart message in MIME format.
--=_alternative 0064DFF986257611_=
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
charset=us-ascii

The following script gives the following ouput
the take a look at the subroutine to better understand how the output is
formatted.

Phone Number Normalizer
Converting (763) Luv Perl to 763-588-7375



# ===============================SCRIPT
PhoneNormalize.pl================================
print "Phone Number Normalizer\n";
my $num = "(763) Luv Perl";
print "Converting $num to ". NormalizePhone($num) ."\n";

# ===========================================================
#This will normalize phone numbers to the format CCC-xxx-xxxx
# ===========================================================
sub NormalizePhone {
my $phone = shift;

# Removing spaces, and other delimiting characters
$phone =~ s/[-()\s+]//g;

# convert all alpha to uppercase
$phone = uc($phone);

# Convert Alpha to Numeric
$phone =~
tr/ABCDEFGHIJKLMNOPQRSTUVWXYZ/22233344455566677778889999/;

# Normalizing phone number
$phone =~ s/(\d{3})(\d{3})(\d{4})/$1-$2-$3/;
return $phone;
}

#
============================================================ ================================



Tony B. Okusanya




From:
"Stanislaw Romanski"
To:
"Barry Brevik" ,

Date:
08/13/2009 12:20 PM
Subject:
Re: Help with regex
Sent by:
activeperl-bounces@listserv.ActiveState.com



Hello,

Maybe you need something like

$text =~ tr/ABCD/2223/;

?

Stanislaw Romanski


----- Original Message -----
From: "Barry Brevik"
To:
Sent: Thursday, August 13, 2009 6:52 PM
Subject: Help with regex


>I am using Active Perl 5.8.8. and Perl Dev Kit 7.0.
>
> I'm writing some code that standardizes the format of phone numbers in a
> database because users have entered them in every way imaginable.
>
> Anyway, some of the phone numbers look like this:
>
> 800-69-VORTEX
>
> ...where the user has entered alpha characters instead of numbers. My
> first thought was to use a hash to do the translation, like this:
>
> %alpha2num =
> (
> 'A' => '2',
> 'B' => '2',
> 'C' => '2',
> 'D' => '3',
> .
> .
> .
>
> ...but since the rest of the routine uses only regular expressions, I
> thought that it would be nice if this type of translation could be
> accomplished with a regex.
>
> I have not been able to think of a way, but I thought I would put it to
> the list to see if anyone else has done this before.
>
> Barry Brevik
> _______________________________________________
> ActivePerl mailing list
> ActivePerl@listserv.ActiveState.com
> To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
>
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs



U.S. BANCORP made the following annotations
------------------------------------------------------------ ---------
Electronic Privacy Notice. This e-mail, and any attachments, contains information that is, or may be, covered by electronic communications privacy laws, and is also confidential and proprietary in nature. If you are not the intended recipient, please be advised that you are legally prohibited from retaining, using, copying, distributing, or otherwise disclosing this information in any manner. Instead, please reply to the sender that you have received this communication in error, and then immediately delete it. Thank you in advance for your cooperation.



------------------------------------------------------------ ---------


--=_alternative 0064DFF986257611_=
Content-Transfer-Encoding: 7bit
Content-Type: text/html;
charset=us-ascii



The following script  gives the
following ouput


the take a look at the subroutine to
better understand how the output is formatted.




Phone Number Normalizer

Converting (763) Luv Perl to 763-588-7375







# ===============================SCRIPT
PhoneNormalize.pl================================


print "Phone Number Normalizer\n";

my $num = "(763) Luv Perl";

print "Converting $num to ".
NormalizePhone($num) ."\n";




# ===========================================================

#This will normalize phone numbers to
the format CCC-xxx-xxxx


# ===========================================================

sub NormalizePhone {

        my
$phone = shift;




        #
Removing spaces, and other delimiting characters


        $phone
=~ s/[-()\s+]//g;


 

        #
convert all alpha to uppercase


        $phone
= uc($phone);        




        #
Convert Alpha to Numeric


        $phone
=~ tr/ABCDEFGHIJKLMNOPQRSTUVWXYZ/22233344455566677778889999/;




        #
Normalizing phone number


        $phone
=~ s/(\d{3})(\d{3})(\d{4})/$1-$2-$3/;


        return
$phone;


}



# ============================================================ ================================







Tony B. Okusanya














From:
"Stanislaw Romanski" <s.romanski@datos.pl>
To:
"Barry Brevik" <BBrevik@StellarMicro.com>,
<activeperl@listserv.ActiveState.com>

Date:
08/13/2009 12:20 PM
Subject:
Re: Help with regex
Sent by:
activeperl-bounces@listserv.ActiveState.com










Hello,



Maybe you need something like



   $text =~ tr/ABCD/2223/;



?



Stanislaw Romanski





----- Original Message -----

From: "Barry Brevik" <BBrevik@StellarMicro.com>

To: <activeperl@listserv.ActiveState.com>

Sent: Thursday, August 13, 2009 6:52 PM

Subject: Help with regex





>I am using Active Perl 5.8.8. and Perl Dev Kit 7.0.

>

> I'm writing some code that standardizes the format of phone numbers
in a

> database because users have entered them in every way imaginable.

>

> Anyway, some of the phone numbers look like this:

>

>  800-69-VORTEX

>

> ...where the user has entered alpha characters instead of numbers.
My

> first thought was to use a hash to do the translation, like this:

>

>  %alpha2num =

>  (

>    'A' => '2',

>    'B' => '2',

>    'C' => '2',

>    'D' => '3',

>    .

>    .

>    .

>

> ...but since the rest of the routine uses only regular expressions,
I

> thought that it would be nice if this type of translation could be

> accomplished with a regex.

>

> I have not been able to think of a way, but I thought I would put
it to

> the list to see if anyone else has done this before.

>

> Barry Brevik

> _______________________________________________

> ActivePerl mailing list

> ActivePerl@listserv.ActiveState.com

> To unsubscribe:


>

_______________________________________________

ActivePerl mailing list

ActivePerl@listserv.ActiveState.com

To unsubscribe:







U.S. BANCORP made the following annotations
------------------------------------------------------------ ---------
Electronic Privacy Notice. This e-mail, and any attachments, contains information that is, or may be, covered by electronic communications privacy laws, and is also confidential and proprietary in nature. If you are not the intended recipient, please be advised that you are legally prohibited from retaining, using, copying, distributing, or otherwise disclosing this information in any manner. Instead, please reply to the sender that you have received this communication in error, and then immediately delete it. Thank you in advance for your cooperation.



------------------------------------------------------------ ---------


--=_alternative 0064DFF986257611_=--

--===============0354187723==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--===============0354187723==--

Re: Help with regex

am 13.08.2009 20:37:45 von Deane.Rothenmaier

This is a multipart message in MIME format.
--===============2056462222==
Content-Type: multipart/alternative;
boundary="=_alternative 0066558A86257611_="

This is a multipart message in MIME format.
--=_alternative 0066558A86257611_=
Content-Type: text/plain; charset="US-ASCII"

Cheap and dirty, cheezy, kindergarteny, but it does what you want:

use strict;
use warnings;

my $phnum = '847-VICTORY';

print "before: \"$phnum\"\n";

$phnum =~ s/[ABC]/2/ig;
$phnum =~ s/[DEF]/3/ig;
$phnum =~ s/[GHI]/4/ig;
$phnum =~ s/[JKL]/5/ig;
$phnum =~ s/[MNO]/6/ig;
$phnum =~ s/[PQRS]/7/ig;
$phnum =~ s/[TUV]/8/ig;
$phnum =~ s/[WXYZ]/9/ig;

# add hyphen
$phnum =~ s/-(\d\d\d)(\d\d\d\d)/-$1-$2/;

print "after: \"$phnum\"\n";


before: "847-VICTORY"
after: "847-842-8679"

Deane Rothenmaier
Programmer/Analyst
Walgreens Corp.
224-542-5150

I think you should profit from the mistakes of others. You don't live long
enough to make them all yourself. -- Lowell Ferguson
--=_alternative 0066558A86257611_=
Content-Type: text/html; charset="US-ASCII"



Cheap and dirty, cheezy, kindergarteny,
but it does what you want:




use strict;

use warnings;



my $phnum = '847-VICTORY';



print "before: \"$phnum\"\n";



$phnum =~ s/[ABC]/2/ig;

$phnum =~ s/[DEF]/3/ig;

$phnum =~ s/[GHI]/4/ig;

$phnum =~ s/[JKL]/5/ig;

$phnum =~ s/[MNO]/6/ig;

$phnum =~ s/[PQRS]/7/ig;

$phnum =~ s/[TUV]/8/ig;

$phnum =~ s/[WXYZ]/9/ig;



# add hyphen

$phnum =~ s/-(\d\d\d)(\d\d\d\d)/-$1-$2/;



print "after: \"$phnum\"\n";





before: "847-VICTORY"

after: "847-842-8679"



Deane Rothenmaier

Programmer/Analyst

Walgreens Corp.

224-542-5150



I think you should profit from the mistakes of others. You don't live long
enough to make them all yourself. -- Lowell Ferguson

--=_alternative 0066558A86257611_=--


--===============2056462222==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--===============2056462222==--

Re: Help with regex

am 13.08.2009 20:50:04 von Bill Luebkert

Deane.Rothenmaier@walgreens.com wrote:
>
> Cheap and dirty, cheezy, kindergarteny, but it does what you want:
>
> use strict;
> use warnings;
>
> my $phnum = '847-VICTORY';
>
> print "before: \"$phnum\"\n";
>
> $phnum =~ s/[ABC]/2/ig;
> $phnum =~ s/[DEF]/3/ig;
> $phnum =~ s/[GHI]/4/ig;
> $phnum =~ s/[JKL]/5/ig;
> $phnum =~ s/[MNO]/6/ig;
> $phnum =~ s/[PQRS]/7/ig;
> $phnum =~ s/[TUV]/8/ig;
> $phnum =~ s/[WXYZ]/9/ig;
>
> # add hyphen
> $phnum =~ s/-(\d\d\d)(\d\d\d\d)/-$1-$2/;
>
> print "after: \"$phnum\"\n";
>
>
> before: "847-VICTORY"
> after: "847-842-8679"

Brute force method. :) But it won't work if there are () or
no - or 2 -'s etc. tr is much more efficient than your multiple
subs.

Logically you would want to :
1) remove all legal punctuation (and a leading 1 if present),
2) convert all alphas to their numeric equivalent,
3) reject anything that isn't 10 digits (possibly also 7 if
local numbers are allowed),
4) format the result the way you like.
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Help with automated script

am 14.08.2009 21:01:02 von Veeramani Selvakumar

--===============0010539243==
Content-Type: multipart/alternative; boundary="0-1049513781-1250276462=:93231"

--0-1049513781-1250276462=:93231
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable

Dear Perl experts,
I have the following requirement which can be done in Perl/VB script whiche=
ver is smarted to do. I am new to perl and am sure i will learn a lot by yo=
ur valuable hints .
requirements are=20
- the log file should be deleted if the files are older than 400 =
days
- the log files from the server should be kept in the backup fold=
er for 185 days.
Script should check the following:
1. Check if a file c:\\emailmoniroing\\logs\EmailLogyymmdd.log exists =
where â€=98yymmddâ€=99 denotes todayâ€=99s date If it does not,=
create a new file.=20
2. For each file in the â€=98c:\\emailmonitor\mainâ€=99 folder, che=
ck file size.
3. If file size=3D0, then move the file from the 'c:\\Emailmonitoring\main'=
folder to the â€=98 c:\\emailmonitoring\backupâ€=99 folder
4. If file size >0, then read/append contents of the file to c:\emailmonito=
r\logs\EmailLogyymmdd.log. Move the file read from the=20
'c:\\Emailmonitoring\main' folder to the â€=98 c:\\emailmonitoring\back=
upâ€=99 folder
5. Delete all files older than 'x'' months from the c:\\emailmonitoring\bac=
kup folder
6. Delete all EmailLogyymmdd.log files older than â€=98xâ€=99 month=
s from the folder â€=98c:\\emailmonitoring\Logsâ€=99
 
Finally,
Set up a windows scheduler task to run the new script every 20 minutes or i=
t can run continuously, sleep and wake up every 5 minutes
 
Could you provide any sample script something to acheive the above. it will=
be very useful to meet our business needs.=20
 
Thanks in advance
regards
mani =0A See the Web's breaking stories, chosen by people lik=
e you. Check out Yahoo! Buzz. http://in.buzz.yahoo.com/
--0-1049513781-1250276462=:93231
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable

top" style=3D"font: inherit;">
Dear Perl experts,

I have the following requirement which can be done in Perl/VB script w=
hichever is smarted to do. I am new to perl and am sure i will learn a lot =
by your valuable hints .

requirements are

- the log file should be deleted if the files are older than=
400 days

- the log files from the server should be kept in the backup=
folder for 185 days.

Script should check the following:

1. Check if a file c:\\emailmoniroing\ ailLogyymmdd.log">\logs\EmailLogyymmdd.log=
 exists where â€=98yymmddâ€=99 denotes todayâ€=99s date If=
it does not, create a new file.

2. For each file in the â€=98c:\ tor/main">\emailmonitor\mainâ€=99 fold=
er, check file size.

3. If file size=3D0, then move the file from the 'c: TRONG>\\Email=
monitoring\main
' folder to the =E2=
€˜ c=
:\\emailmonitoring\backup
â€=99 folder

4. If file size >0, then read/append contents of the file to c:\=
e
=3D#0000ff>mailmonitor\logs\EmailLogyymmdd.log. Move the =
file read from the

'c: f>\\Emailmonitoring\main' folder to=
the â€=98 c: or=3D#0000ff>\\emailmonitoring\backupâ€=99 folder

5. Delete all files older than 'x'' months from the c: =3D"file://emailmonitoring/backup">\\emailmonitoring\=
backup
folder

6. Delete all EmailLogyymmdd.log files older than â€=98xâ€=99 =
months from the folder â€=98c: Logs">\\emailmonitoring\Logsâ€=99 >
 

Finally,

Set up a windows scheduler task to run the new script every 20 minutes=
or it can run continuously, sleep and wake up every 5 minutes

 

Could you provide any sample script something to acheive the above. it=
will be very useful to meet our business needs.

 

Thanks in advance

regards

mani


> Looking for local information? Find it on hoo.com/tagline_local_1/*http://in.local.yahoo.com/" target=3D"_blank"> Yah=
oo! Local

--0-1049513781-1250276462=:93231--

--===============0010539243==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--===============0010539243==--